package dice.sinanya.dice.game;

import dice.sinanya.dice.MakeNickToSender;
import dice.sinanya.entity.EntityTypeMessages;
import dice.sinanya.entity.EntityWelcome;
import dice.sinanya.exceptions.NotEnableException;
import dice.sinanya.exceptions.NotEnableInGroupException;
import dice.sinanya.exceptions.OnlyManagerException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static dice.sinanya.system.MessagesSystem.loginInfo;
import static dice.sinanya.system.MessagesWelcome.welcomes;
import static dice.sinanya.tools.getinfo.GetMessagesProperties.entityGame;
import static dice.sinanya.tools.getinfo.Welcome.insertWelcome;
import static dice.sinanya.tools.makedata.Sender.sender;

/**
 * @author SitaNya
 * 日期: 2019-06-15
 * 电子邮箱: sitanya@qq.com
 * 维护群(QQ): 162279609
 * 有任何问题欢迎咨询
 * 类说明: 今日人品类，其实不是很想做……
 */
public class Welcome implements MakeNickToSender {
    private final EntityTypeMessages entityTypeMessages;

    private final Pattern atMe = Pattern.compile("^[ ]*\\[");

    public Welcome(EntityTypeMessages entityTypeMessages) {
        this.entityTypeMessages = entityTypeMessages;
    }

    public void set() throws NotEnableException, OnlyManagerException, NotEnableInGroupException {
        Matcher atMeMatcher = atMe.matcher(entityTypeMessages.getMsgGet().getMsg());
        if (atMeMatcher.find() && !entityTypeMessages.getMsgGet().getMsg().contains(String.valueOf(loginInfo.getLoginId()))) {
            return;
        }
        if (entityTypeMessages.getMsgSender().GETTER.getGroupMemberInfo(entityTypeMessages.getFromGroupString(), entityTypeMessages.getFromQqString()).getPowerType().isMember()) {
            throw new OnlyManagerException(entityTypeMessages);
        }
        String msg = entityTypeMessages.getMsgGet().getMsg().trim().replaceFirst(".*[,，.。][ ]*welcome[ ]*", "").trim();
        String tagMsg = entityTypeMessages.getMsgGet().getMsg().trim();
        if (tagMsg.contains(":at") && !tagMsg.contains(String.valueOf(loginInfo.getLoginId()))) {
            return;
        }
        long groupId = entityTypeMessages.getFromGroup();
        if (msg.equals("close")) {
            String text = "";
            if (welcomes.containsKey(groupId)) {
                text = welcomes.get(groupId).getText();
            }
            if (!entityGame.isWelcomeSwitch()) {
                throw new NotEnableException(entityTypeMessages);
            }
            EntityWelcome entityWelcome = new EntityWelcome(false, text);
            insertWelcome(entityTypeMessages.getFromGroup(), entityWelcome);
            welcomes.put(groupId, entityWelcome);
            sender(entityTypeMessages, entityGame.getWelcomeClose(entityTypeMessages.getFromGroupString()));
        } else if (msg.equals("open")) {
            String text = "";
            if (welcomes.containsKey(groupId)) {
                text = welcomes.get(groupId).getText();
            }
            if (!entityGame.isWelcomeSwitch()) {
                throw new NotEnableException(entityTypeMessages);
            }
            EntityWelcome entityWelcome = new EntityWelcome(true, text);
            insertWelcome(entityTypeMessages.getFromGroup(), entityWelcome);
            welcomes.put(groupId, entityWelcome);
            sender(entityTypeMessages, entityGame.getWelcomeOpen(entityTypeMessages.getFromGroupString()));
        } else {
            checkEnable();
            EntityWelcome entityWelcome = new EntityWelcome(true, msg);
            insertWelcome(groupId, entityWelcome);
            welcomes.put(groupId, entityWelcome);
            sender(entityTypeMessages, entityGame.getWelcomeInsert(entityTypeMessages.getFromGroupString()));
        }
    }

    private void checkEnable() throws NotEnableException, NotEnableInGroupException {
        if (!entityGame.isWelcomeSwitch()) {
            throw new NotEnableException(entityTypeMessages);
        }
        if (!welcomes.getOrDefault(entityTypeMessages.getFromGroup(), new EntityWelcome(true, "")).isEnable()) {
            throw new NotEnableInGroupException(entityTypeMessages);
        }
    }
}
